/*
 * Team Java
 * Michael Pogson, Michael Satranjr, Stephen Hunter
 * Project 1
 * 4/26/12
 */

package httpclient;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

import controller.Manager;

import queue.ListQueue;
import queue.QueueADT;

public class Retriever implements Callable<Website>, Runnable
{
	/**
	 * The backing store of this object.
	 */
	//QueueADT<URL> my_url_queue;
	URL my_url;
	
	/**
	 * This is the manager which this thread will report back to when it has retrieved a URL.
	 */
	Manager my_manager;
	boolean shouldStop;
	
	public Retriever(URL the_url) 
	{
		my_url = the_url;
		//my_url_queue = new ListQueue<URL>();	
		//my_manager = m;
	}
	
	public Retriever(Manager man, URL the_url)
	{
		my_url = the_url;
		my_manager = man;
		shouldStop = false;
	}
	
	public void setURL(URL the_url)
	{
		my_url = the_url;
	}
	
	/**
	 * @return Returns a string buffer of the HTML content recieved from the URL processed.
	 * @throws IOException Will throw an IOException if the URL cannot be accessed.
	 * @throws NullPointerException Will throw a null pointer exception if there is nothing left in the queue.
	 */
	public Website dequeNextURLContent() throws IOException, NullPointerException
	{
		StringBuffer content_buffer = new StringBuffer();
		URL content_url = my_url;
		
		InputStream in;
		in = content_url.openStream();
		
		int char_offset = in.read();
		
		while (char_offset != -1)
		{
			content_buffer.append((char)char_offset);
			char_offset = in.read();
		}

		
		return new Website(content_buffer.toString(), content_url);
	}

	@Override
	public Website call() throws Exception {
		
		return dequeNextURLContent();
	}

	@Override
	public void run() {
		while(!shouldStop) {
			QueueADT<URL> urls = my_manager.grabURLSToRetrieve(1);
			List<Website> websites = new LinkedList<Website>();
			while (!urls.isEmpty())
			{
				try{
					setURL(urls.deque());
					websites.add(call());} 
				catch(Exception e){
					//Try again
					continue;
				}
			}
			for (Website web : websites)
			{
				if(!web.my_url.toString().equals( "http://questioneverything.typepad.com/")) {
					my_manager.enqueWebsite(web);
				}
			}
		}
	}
	
	public void stop()
	{
		shouldStop = true;
	}
}